﻿using BussinessObject.ObjectTwo;
using Class_Entity.ObjectTwo;
using ProjectHK3.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectHK3.ObjectTwo
{
    public partial class OrderListDetail : System.Web.UI.Page
    {
        B_Search gx = new B_Search();
        Orders jx = new Orders();
        B_LoadDrop ex = new B_LoadDrop();
        B_Update zx = new B_Update();
        B_Cal kx = new B_Cal();
        C_Calculator lx = new C_Calculator();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load();
            }
        }
        public void load()
        {        
            jx = gx.ORDERSDETAIL(int.Parse(Session["Order"].ToString()));
            DropDownList1.DataSource = ex.city();
            DropDownList1.DataTextField = "CITY_NAME";
            DropDownList1.DataValueField = "CITY_ID";
            DropDownList1.SelectedValue = jx.SENDER_CITY.ToString();
            DropDownList1.DataBind();

            DropDownList2.DataSource = ex.city();
            DropDownList2.DataTextField = "CITY_NAME";
            DropDownList2.DataValueField = "CITY_ID";
            DropDownList2.SelectedValue = jx.RECEIVER_CITY.ToString();
            DropDownList2.DataBind();

            DropDownList4.DataSource = ex.service();
            DropDownList4.DataTextField = "SERVICE_NAME";
            DropDownList4.DataValueField = "SERVICE_ID";
            DropDownList4.SelectedValue = jx.SERVICE_ID.ToString();
            DropDownList4.DataBind();

            DropDownList5.DataSource = ex.Bran();
            DropDownList5.DataTextField = "BRANCH_NAME";
            DropDownList5.DataValueField = "BRANCH_ID";
            DropDownList5.SelectedValue = jx.BRANCH_ID_FROM.ToString();
            DropDownList5.DataBind();

            DropDownList6.DataSource = ex.Bran();
            DropDownList6.DataTextField = "BRANCH_NAME";
            DropDownList6.DataValueField = "BRANCH_ID";
            DropDownList6.SelectedValue = jx.BRANCH_ID_TO.ToString();
            DropDownList6.DataBind();

            TextBox1.Text = jx.ORDER_NUMBER;
            TextBox19.Text = jx.ORDER_DATE;
            DropDownList7.SelectedValue = jx.STATUS;

            TextBox2.Text = jx.SENDER_NAME;
            TextBox3.Text = jx.SENDER_PHONE;
            TextBox4.Text = jx.SENDER_ADDRESS;

            TextBox5.Text = jx.RECEIVER_NAME;
            TextBox6.Text = jx.RECEIVER_PHONE;
            TextBox7.Text = jx.RECEIVER_ADDRESS;

            TextBox8.Text = jx.MAIL_CODE;
            TextBox9.Text = jx.WEIGHT.ToString();
            TextBox18.Text = jx.DESCRIPTION;
            TextBox11.Text = jx.TOTAL_DELIVERY_FEE.ToString();
            TextBox12.Text = jx.TOTAL_PAID.ToString();

        }
        public void update()
        {

            jx.ORDER_NUMBER = TextBox1.Text;
            jx.ORDER_DATE = DateTime.Now.ToString("dd/MM/yyyy");
            jx.IS_INTERNAL_DELIVERY = 0;
            jx.SENDER_NAME = TextBox2.Text;
            jx.ACCOUNT_ID = int.Parse(Session["Userid"].ToString());
            jx.SENDER_PHONE = TextBox3.Text;
            jx.SENDER_ADDRESS = TextBox4.Text;
            jx.SENDER_CITY = int.Parse(DropDownList1.SelectedValue.ToString());
            jx.RECEIVER_NAME = TextBox5.Text;
            jx.RECEIVER_PHONE = TextBox6.Text;
            jx.RECEIVER_ADDRESS = TextBox7.Text;
            jx.RECEIVER_CITY = int.Parse(DropDownList2.SelectedValue.ToString());
            jx.MAIL_CODE = TextBox8.Text;
            jx.WEIGHT = float.Parse(TextBox9.Text);
            jx.DESCRIPTION = TextBox18.Text;
            jx.SERVICE_ID = int.Parse(DropDownList4.SelectedValue.ToString());
            jx.BRANCH_ID_FROM = int.Parse(DropDownList5.SelectedValue.ToString());
            jx.BRANCH_ID_TO = int.Parse(DropDownList6.SelectedValue.ToString());
            jx.TOTAL_DELIVERY_FEE = float.Parse(TextBox10.Text);
            jx.TOTAL_PAID = float.Parse(TextBox12.Text);
            jx.STATUS = DropDownList7.SelectedValue.ToString();
            jx.ORDER_ID = int.Parse(Session["Order"].ToString());
            jx.RECEIVE_DATE = TextBox20.Text;
            if (zx.UpdateOrderA(jx) == true)
            {
                Alert.Show("Update Successful");
            }
            else
            {
                Alert.Show("Update Fail");
            }
            
        }
        public void cal()
        {
            lx = kx.Dis(int.Parse(DropDownList5.SelectedValue.ToString()), int.Parse(DropDownList6.SelectedValue.ToString()));
            int vala;
            vala = int.Parse(lx.KC.ToString());
            TextBox10.Text = vala.ToString();
            lx = kx.FEE(vala, int.Parse(DropDownList4.SelectedValue.ToString()));
            float a1, a2, a3;
            a1 = lx.FEE_PER_KG;
            a2 = lx.FEE_PER_KM;
            a3 = (vala * a2) + (a1 * float.Parse(TextBox9.Text.ToString()));
            TextBox11.Text = a3.ToString();
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
            cal();
            RequiredFieldValidator20.Visible = true;
            RegularExpressionValidator5.Visible = true;
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBox10.Text.ToString() == "")
            {
                cal();
                
            }
            else
            {
                if (TextBox12.Text.ToString() == "")
                {
                    cal();
                    if (int.Parse(TextBox12.Text.ToString()) < int.Parse(TextBox11.Text.ToString()))
                    {
                        Alert.Show("'Total Paid' greater than or equal to Fee Charge.");
                    }
                    else
                    {
                        TextBox13.Text = (int.Parse(TextBox12.Text.ToString()) - int.Parse(TextBox11.Text.ToString())).ToString();
                    }
                }
                else
                {
                    update();
                }
            }
        }

        protected void TextBox12_TextChanged(object sender, EventArgs e)
        {
           
        }

        protected void TextBox10_TextChanged(object sender, EventArgs e)
        {
          
        }
    }
}